package cn.vawn.base.controller;

import cn.vawn.base.consts.AppConsts;
import cn.vawn.base.consts.Contants;
import cn.vawn.base.dto.RequestPageDTO;
import cn.vawn.base.dto.ResponseResult;
import cn.vawn.base.entity.AdminUser;
import cn.vawn.base.util.DateUtil;
import cn.vawn.base.util.ParamUtil;
import cn.vawn.base.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

/**
 * Add by jianhan on 2018/5/4
 */
public class BaseController {

    @Autowired
    private HttpServletRequest request;


    @ExceptionHandler
    @ResponseBody
    public ResponseResult<Void> exp(HttpServletRequest request, HttpServletResponse response, Exception e) {

        ResponseResult msg = new ResponseResult();

        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Type", "application/json;charset=UTF-8");

        msg.setMessage("请求错误，请稍后重试");
        msg.setState(-90);
        return msg;
    }
    /**
     * 共通的错误响应对象
     * @param message
     * @return
     */
    public ResponseResult getCommonErrorResponse(String message) {
        return new ResponseResult(AppConsts.RESPONSE_STATUS_ERROR, message);
    }

    /**
     * 获取当前登录用户
     * @return
     */
    public AdminUser getCurrentUser() {
        return (AdminUser) request.getSession().getAttribute(Contants.LOGIN_USER_SESSION_KEY);
    }

    /**
     * 获取语言
     * @return
     */
    public String getLanguage() {
        String language = StringUtil.trim(request.getHeader("language"));
        return StringUtil.isEmpty(language) ? StringUtil.trim(request.getParameter("language")) : language;
    }

    /**
     * 获取分页对象
     * @return
     */
    public RequestPageDTO getPage() {

        RequestPageDTO requestPageDTO = new RequestPageDTO();

        String pageNum_str = StringUtil.trim(request.getParameter("pageNumber"));
        int pageNum = StringUtil.isEmpty(pageNum_str) ? 0 : Integer.parseInt(pageNum_str);

        String pageSize_str = StringUtil.trim(request.getParameter("pageSize"));
        int pageSize = StringUtil.isEmpty(pageSize_str) ? 0 : Integer.parseInt(pageSize_str);

        String sort = StringUtil.trim(request.getParameter("sort"));

        String desc = StringUtil.trim(request.getParameter("desc"));

        requestPageDTO.setPageNumber(pageNum);
        requestPageDTO.setPageSize(pageSize);
        requestPageDTO.setSort(sort);
        requestPageDTO.setDesc(desc);

        return requestPageDTO;
    }

    public String getClientIp() {
        return ParamUtil.getClientIp(request);
    }


    public Map<String, String[]> getParaMap(HttpServletRequest request) {
        return request.getParameterMap();
    }

    private String getValue(HttpServletRequest request, String paramName) {
        return request.getParameter(paramName);
    }

    protected String getPara(HttpServletRequest request, String paramName) {
        return getValue(request, paramName);
    }

    protected Integer getParaToInt(HttpServletRequest request, String paramName) {
        String value = getValue(request, paramName);
        if (StringUtil.notEmpty(value)) {
            return Integer.parseInt(value);
        }
        return null;
    }

    protected Long getParaToLong(HttpServletRequest request, String paramName) {
        String value = getValue(request, paramName);
        if (StringUtil.notEmpty(value)) {
            return Long.parseLong(value);
        }
        return null;
    }

    protected Float getParaToFloat(HttpServletRequest request, String paramName) {
        String value = getValue(request, paramName);
        if (StringUtil.notEmpty(value)) {
            return Float.parseFloat(value);
        }
        return null;
    }

    protected Double getParaToDouble(HttpServletRequest request, String paramName) {
        String value = getValue(request, paramName);
        if (StringUtil.notEmpty(value)) {
            return Double.parseDouble(value);
        }
        return null;
    }

    protected Boolean getParaToBoolean(HttpServletRequest request, String paramName) {
        String value = getValue(request, paramName);
        if (StringUtil.notEmpty(value)) {
            return Boolean.parseBoolean(value);
        }
        return null;
    }

    protected Date getParaToDate(HttpServletRequest request, String paramName) {
        String value = getValue(request, paramName);
        if (StringUtil.notEmpty(value)) {
            return DateUtil.parseDate(request.getParameter(paramName));
        }
        return null;
    }

    protected Date getParaToDatetime(HttpServletRequest request, String paramName) {
        String value = getValue(request, paramName);
        if (StringUtil.notEmpty(value)) {
            return DateUtil.parseDateTime(request.getParameter(paramName));
        }
        return null;
    }

    protected Date getParaToDate(HttpServletRequest request, String paramName, String pattern) {
        String value = getValue(request, paramName);
        if (StringUtil.notEmpty(value)) {
            return DateUtil.parseDate(request.getParameter(paramName), pattern);
        }
        return null;
    }

    protected String[] getParaValues(HttpServletRequest request, String paramName) {
        return request.getParameterValues(paramName);
    }
}
